<?php defined( '_VALID_MOS' ) or die( 'Restricted access' );?>
<?php

$sql = "SHOW COLUMNS FROM #__jcs_plans";
$database->setQuery($sql);
$rows = $database->loadResultArray();

$sql = "ALTER TABLE `#__jcs_plans` MODIFY COLUMN `category` TEXT;";
$database->setQuery($sql);
$database->query();

if(!in_array('discount', $rows))
{
    $sql = "ALTER TABLE `#__jcs_plans` ADD COLUMN `discount` FLOAT(10,2) NOT NULL DEFAULT 0.00 AFTER `ctime`;";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('invisible', $rows))
{
    $sql = "ALTER TABLE `#__jcs_plans` ADD COLUMN `invisible` TINYINT(1) UNSIGNED NOT NULL DEFAULT 0 AFTER `discount`;";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('ordering', $rows))
{
    $sql = "ALTER TABLE `#__jcs_plans` ADD COLUMN `ordering` TINYINT(3) UNSIGNED NOT NULL DEFAULT 0 AFTER `invisible`;";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('catid', $rows))
{
    $sql = "ALTER TABLE `#__jcs_plans` ADD COLUMN `catid` INTEGER(11) UNSIGNED NOT NULL DEFAULT 0 AFTER `ordering`;";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('finish_msg', $rows))
{
    $sql = "ALTER TABLE `#__jcs_plans` ADD COLUMN `finish_msg` TEXT AFTER `catid`;";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('gid', $rows))
{
    $sql = "ALTER TABLE `#__jcs_plans` ADD COLUMN `gid` INTEGER UNSIGNED NOT NULL DEFAULT 18 AFTER `finish_msg`";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('usertype', $rows))
{
    $sql = "ALTER TABLE `#__jcs_plans` ADD COLUMN `usertype` VARCHAR(45) NOT NULL DEFAULT 'Registered' AFTER `gid`";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('data_fixed', $rows))
{
    $sql = "ALTER TABLE `#__jcs_plans` ADD COLUMN `data_fixed` TINYINT(1) UNSIGNED NOT NULL DEFAULT 0 AFTER `usertype`";
    $database->setQuery($sql);
    $database->query();
}



$sql = "SHOW COLUMNS FROM #__jcs_user_subscr";
$database->setQuery($sql);
$rows2 = $database->loadResultArray();

if(!in_array('gateway', $rows2))
{
    $sql = "ALTER TABLE `#__jcs_user_subscr` ADD COLUMN `gateway` VARCHAR(20) NOT NULL DEFAULT '' AFTER `access_limit`;";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('gateway_id', $rows2))
{
    $sql = "ALTER TABLE `#__jcs_user_subscr` ADD COLUMN `gateway_id` VARCHAR(200) NOT NULL DEFAULT '' AFTER `gateway`;";
    $database->setQuery($sql);
    $database->query();
}
if(!in_array('created', $rows2))
{
    $sql = "ALTER TABLE `#__jcs_user_subscr` ADD COLUMN `created` DATETIME NOT NULL AFTER `gateway_id`";
    $database->setQuery($sql);
    $database->query();
    $sql = "UPDATE `#__jcs_user_subscr` SET created = ctime";
    $database->setQuery($sql);
    $database->query();
}
/*
$sql = "SELECT id FROM #__je_config WHERE component = 'com_jcs' AND name = 'image_call'";
$database->setQuery($sql);
$id = $database->loadResult();
$o['lable']   = 'Image Call';
$o['type']    = 'text';
$o['values']  = 'http://mysite.com/affiliate/index.php?amount=[price]&id=[unique_id]';
$o['descr']  = 'This image will be loaded on success payment. YOu can use variables [price], [order_id], [unique_id], [title]. Where order_id is and ID of the record and unique_id is combination of payment processor ID and order ID';
$o['fieldset']= 'General';
if(!$id) JEConfig::set('general.image_call', '', $o);
*/

$sql = "SELECT id FROM #__je_config WHERE component = 'com_jcs' AND name = 'coupons'";
$database->setQuery($sql);
$id = $database->loadResult();
$o['lable']   = 'Coupons';
$o['type']    = 'textarea';
$o['values']  = '';
$o['descr']  = 'Each coupon is a new line. Coupon number and discount separated by + (E.g. SKJ67-L453S+12 where SKJ67-L453S is a coupon number and 12 is a procent discount)';
$o['fieldset']= 'General';
if(!$id) JEConfig::set('general.coupons', 'HDG7GD093+12', $o);

if(!JEConfig::get('general.register'))
{
    $sql = "INSERT INTO `#__je_config` (`id`,`component`,`section`,`lable`,`name`,
            `type`,`description`,`values`,`selected`,`hidden`,`fieldset`) VALUES
            (NULL,'com_jcs','general','Register Means','register','selectlist',
            'What component to handel registration with',
            'JconentSubscription, JUser, Community Builder, Core Joomla',
            'JconentSubscription','N','General')";
    $database->setQuery($sql);
    $database->query();
}

$database->setQuery("SELECT id FROM #__components WHERE name= '.Subscription Plans' AND parent = 0 AND `link` = 'option=com_jcs'");
$id = $database->loadResult();

$database->setQuery("SELECT id FROM #__components WHERE parent='$id' AND name = 'Reports'");
$id2 = $database->loadResult();
if($id2)
{
    $sql = "UPDATE #__components SET admin_menu_img = '../administrator/components/com_jcs/reports16.png' WHERE parent='$id' AND name = 'Reports'";
}
else
{
    $sql = "INSERT INTO #__components (name, parent, admin_menu_link, `option`, admin_menu_img) VALUES (
    'Reports', '$id', 'option=com_jcs&task=report', 'com_jcs', '../administrator/components/com_jcs/reports16.png'
    )";
}
$database->setQuery($sql);
$database->query();

$database->setQuery("SELECT id FROM #__components WHERE parent='$id' AND name = 'Categories'");
$id2 = $database->loadResult();
if($id2)
{
    $sql = "UPDATE #__components SET admin_menu_img = 'js/ThemeOffice/categories.png' WHERE parent='$id' AND name = 'Categories'";
}
else
{
    $sql = "INSERT INTO #__components (name, parent, admin_menu_link, `option`, admin_menu_img) VALUES (
    'Categories', '$id', 'option=com_categories&section=com_jcs_plans', 'com_jcs', 'js/ThemeOffice/categories.png'
    )";
}
$database->setQuery($sql);
$database->query();

$database->setQuery("SELECT COUNT(*) FROM #__categories WHERE section='com_jcs_plans'");
$id2 = $database->loadResult();
if(!$id2)
{
    $sql = "INSERT INTO #__categories (`title`, `name`, `section`, 
    `description`, `published`, `ordering`) VALUES ('General Subscription Plans', 
    'General Subscription Plans', 'com_jcs_plans', '<p>Below is the list of 
    available subscriptions. Please carefully read description to choose 
    appropriate one.</p>', 1, 1)";
    $database->setQuery($sql);
    $database->query();
    $sql = "UPDATE #__jcs_plans SET catid = {$id2}";
    $database->setQuery($sql);
    $database->query();
}

//@unlink(__FILE__);
?>
